package team.stride.database.crud.playlists;

import java.util.ArrayList;
import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import team.stride.utils.UtilsStride;
import android.app.Activity;
import android.app.Dialog;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class BDPlaylistsActivity extends Activity
{
	private static final int INSERT_ID = 0;
	private static final int INSERT_DIALOG = 0;
	private List<PlaylistsItem> recordsList;
	private ListView listLV;
	private PlaylistsAdapter adapter;
	private Dialog insertDialog;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bd_playlists_layout);

		initDialog();
		listLV = (ListView) findViewById(R.id.playlistsListView);

		recordsList = new ArrayList<PlaylistsItem>();
		adapter = new PlaylistsAdapter(this, recordsList);
		listLV.setAdapter(adapter);
	}

	private void initDialog()
	{
		insertDialog = new Dialog(this);
		insertDialog.setContentView(R.layout.bd_playlists_insert);
		insertDialog.setTitle("Insert a Playlist");
		insertDialog.setCancelable(true);
		insertDialog.setCanceledOnTouchOutside(true);
		
		final EditText name = (EditText) insertDialog.findViewById(R.id.playlistsInsertName);
		final EditText details = (EditText) insertDialog.findViewById(R.id.playlistsInsertDetails);
		
		Button okBtn = (Button) insertDialog.findViewById(R.id.playlistsInsertOk);
		okBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				PlaylistsItem item = new PlaylistsItem(name.getText().toString(), details.getText().toString());
				UtilsStride.insertInDB(StrideDB.MATCH_P, item);
				recordsList.add(item);
				adapter.notifyDataSetChanged();
				insertDialog.hide();
			}
		});
	}

	@Override
	public void onResume()
	{
		super.onResume();
		recordsList.clear();
		Cursor cursor = managedQuery(StrideDB.CONTENT_URI_PLAYLISTS, null, null, null, null);
		while (cursor.moveToNext())
		{
			final int name_index = cursor.getColumnIndex(StrideDB.NAME_P);
			final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_P);

			String name = cursor.getString(name_index);
			String details = cursor.getString(details_index);

			recordsList.add(new PlaylistsItem(name, details));
		}
		cursor.close();
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0, INSERT_ID, Menu.FIRST, "Insert");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if (item.getItemId() == INSERT_ID)
		{
			showDialog(INSERT_DIALOG);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch (id)
		{
		case INSERT_DIALOG:
			return insertDialog;
		}
		return null;
	}
}
